package com.example.finalexamcovid_19situations;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class liuJiaQi extends AppCompatActivity  implements SensorEventListener{
    private SensorManager mSensorManager;
    private Context mContext;
    Button b1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.liu_jia_qi);

        b1=(Button)findViewById(R.id.b1);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(liuJiaQi.this,
                        "作者：刘佳奇\n学号：2006030213\n小组：潘洪立小组\n内容：摇一摇祈福",
                        Toast.LENGTH_SHORT).show();
            }
        });
        // 获取传感器管理服务
        mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onStop() {
        mSensorManager.unregisterListener(this);// 退出界面后，把传感器释放。
        super.onStop();
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        int sensorType = event.sensor.getType();

        float[] values = event.values;
        if (sensorType == Sensor.TYPE_ACCELEROMETER) {
            int ROCKPOWER = 15;
            if ((Math.abs(values[0]) > ROCKPOWER ||
                    Math.abs(values[1]) > ROCKPOWER ||
                    Math.abs(values[2]) > ROCKPOWER)) {
                System.out.println("YYYYYYYYYYYY   Math.abs(values[0]=" +
                        Math.abs(values[0]) + "     Math.abs(values[1]=" +
                        Math.abs(values[1]) + "       Math.abs(values[2]" +
                        Math.abs(values[2]));
                Toast.makeText(liuJiaQi.this,
                        "感谢您为疫情退去祈福❀",
                        Toast.LENGTH_SHORT).show();

            }
        }
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }
}